<template>
    <div>
        <div class="ui-upload-images" v-for="(item, index) in uploadList" :key="index">
            <template v-if="item.status === 'finished'">
                <img :src="item.url">
                <div class="ui-upload-images-cover">
                    <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
                    <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
                </div>
            </template>
            <template v-else>
                <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
            </template>
        </div>
        <Upload
            ref="upload"
            type="drag"
            accept="image/*"
            :action="ajax"
            :headers="headers"
            :show-upload-list="false"
            :default-file-list="defaultFileList"
            :on-success="handleSuccess"
            :format="format"
            :max-size="maxSize"
            :on-format-error="handleFormatError"
            :on-exceeded-size="handleMaxSize"
            :before-upload="handleBeforeUpload"
            :multiple="multiple"
            style="display: inline-block;"
        >
            <slot name="button">
                <div style="width: 58px;height:58px;line-height: 58px;">
                    <Icon type="md-camera" size="20"></Icon>
                </div>
            </slot>
        </Upload>
        <Modal title="图片预览" v-model="visible">
            <img :src="viewUrl" v-if="visible" style="width: 100%">
        </Modal>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import { host } from '@/config/index';
export default {
    name: 'ImageUploads',
    props: {
        value: {
            type: Array,
            default: function() {
                return [];
            }
        },
        format: { // 图片格式
            type: Array,
            default: function() {
                return ['jpg','jpeg','png', 'gif', 'bmp'];
            }
        },
        maxSize: { // 图片大小限制，KB为单位
            type: Number,
            default: 2048
        },
        limit: { // 上传数量限制
            type: Number,
            default: 5
        },
        multiple: { // 是否支持多选
            type: Boolean,
            default: false
        },
        defaultFileList: {
            type: Array,
            default: function() {
                return [];
            }
        }
    },
    data() {
        return {
            ajax: host + '/attachmentApi/upload',
            viewUrl: '',
            visible: false,
            uploadList: [],
            headers: {}
        }
    },
    watch: {
        defaultFileList(newval) {
            if(this.uploadList.length === 0 && newval.length !== 0) {
                this.uploadList = newval.map(item => {
                    return {
                        status: "finished",
                        url: item.url
                    }
                });
            }
        }
    },
    methods: {
        handleView (url) {
            this.viewUrl = url;
            this.visible = true;
        },
        handleRemove (file) {
            const fileList = this.$refs.upload.fileList;
            this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
            this.handleSyncResult();
            this.$emit('input', this.uploadList);
        },
        handleSuccess (res, file, fileList) {
            file.url = res.data.full_url;
            this.$emit('input', this.uploadList);
        },
        handleFormatError (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: file.name + ' 的格式不正确， 请选择'+ this.format.join('、') +'格式的文件。'
            });
        },
        handleMaxSize (file) {
            // 单位换算
            let _Mb = this.maxSize / 1024;
            let f = _Mb.toString().split(".");
            if(f.length > 1) {
                _Mb = f[0] + '.' + f[1].substring(0, 2);
            }
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: file.name + ' 文件太大了， 请选择'+ _Mb +'M以内的文件。'
            });
        },
        handleBeforeUpload () {
            // 如果filelist不等于uploadlist，先赋值
            this.handleSyncResult();
            // 以下是校验上传数量限制
            const check = this.uploadList.length < this.limit;
            if (!check) {
                this.$Notice.warning({
                    title: '最多可以上传 '+ this.limit +' 个文件。'
                });
            }
            return check;
        },
        handleSyncResult () {
            // 同步数据
            if(this.$refs.upload.fileList != this.uploadList){
                this.uploadList = this.$refs.upload.fileList;
            }
        },
        // 提供清空上传列表的method，需要在父组件调用此method
        clearFiles() {
            this.$refs.upload.clearFiles();
            this.handleSyncResult();
            this.$emit('input', []);
        }
    },
    mounted () {
        if(Cookies.get('token')) {
            this.headers.Authorization = Cookies.get('token');
        }
        this.$nextTick(() => {
            this.handleSyncResult();
        })
    }
}
</script>

<style scoped>
.ui-upload-images {
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 58px;
    border: 1px solid rgba(0,0,0,.2);
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    margin-right: 4px;
}
.ui-upload-images img {
    margin-top: -3px;
    max-width: 100%;
    max-height: 100%;
    vertical-align: middle;
}
.ui-upload-images-cover {
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
}
.ui-upload-images:hover .ui-upload-images-cover {
    display: block;
}
.ui-upload-images-cover i {
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
}
</style>